//
//  ST_AttractedSupplementCell.swift
//  btvideo
//
//  Created by mac on 2020/4/28.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift
import HandyJSON


/**
 被你吸引 为空 的另一种Cell
 */
class ST_AttractedSupplementCell: UICollectionViewCell {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .white
        contentView.cornerRadius = 20
        
        contentView.addSubview(pec_image_view)
        pec_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(45)
            make.top.left.equalToSuperview().offset(10)
        }
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(pec_image_view.snp.right).offset(10)
            make.top.equalTo(pec_image_view).offset(2)
        }
        
        contentView.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.left.equalTo(pec_image_view.snp.right).offset(10)
            make.top.equalTo(titleLabel.snp.bottom).offset(3)
        }
        
        contentView.addSubview(add_btn)
        add_btn.snp.makeConstraints { (make) in
            make.width.equalTo(58)
            make.height.equalTo(32)
            make.top.equalToSuperview().offset(14)
            make.right.equalToSuperview().offset(-10)
        }
        
        
        let imageView0 = UIImageView()
        imageView0.contentMode = .scaleAspectFill
        imageView0.cornerRadius = 20
        
        contentView.addSubview(imageView0)
        imageView0.snp.makeConstraints { (make) in
            make.left.equalTo(pec_image_view)
            make.top.equalTo(pec_image_view.snp.bottom).offset(15)
            make.size.equalTo(ST_AttractedSupplementCell.image_item_view_size)
        }
        
        
        let imageView1 = UIImageView()
        imageView1.contentMode = .scaleAspectFill
        imageView1.cornerRadius = 20
        
        contentView.addSubview(imageView1)
        imageView1.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(imageView0)
            make.size.equalTo(ST_AttractedSupplementCell.image_item_view_size)
        }
        
        let imageView2 = UIImageView()
        imageView2.contentMode = .scaleAspectFill
        imageView2.cornerRadius = 20
        
        contentView.addSubview(imageView2)
        imageView2.snp.makeConstraints { (make) in
            make.left.equalTo(imageView0)
            make.top.equalTo(imageView0.snp.bottom).offset(5)
            make.size.equalTo(ST_AttractedSupplementCell.image_item_view_size)
        }
        
        let imageView3 = UIImageView()
        imageView3.contentMode = .scaleAspectFill
        imageView3.cornerRadius = 20
        
        contentView.addSubview(imageView3)
        imageView3.snp.makeConstraints { (make) in
            make.right.equalTo(imageView1)
            make.top.equalTo(imageView1.snp.bottom).offset(5)
            make.size.equalTo(ST_AttractedSupplementCell.image_item_view_size)
        }
        
        imageViews = [imageView0,imageView1,imageView2,imageView3]
        
        imageViews.forEach { (iv) in
            
            let tap = UITapGestureRecognizer.init(target: self, action: #selector(preview_handle(gez:)))
            
            iv.addGestureRecognizer(tap)
            
            iv.isUserInteractionEnabled = true
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    MARK: - Properties
    static var image_item_view_size:CGSize = {
        let w = (ScreenWidth - 20 * 2 - 5)/2.0
        let h = w/165.0*220
        return CGSize.init(width: w, height: h)
    }()

    fileprivate lazy var pec_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
          control.cornerRadius = 10
          return control
    }()
    
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#BEC4CF")
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    

    fileprivate lazy var add_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("添加")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        control.backgroundColor = Color.init(hexString: "#FEC207")
        control.cornerRadius = 15
        control.addTarget(self, action: #selector(add_pec_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate var imageViews = [UIImageView]()
    
//    MARK: - Bind Model
    var model = ST_AttractedDataModel(){
        didSet{
            
            for (idx,user) in model.feature_user.enumerated(){
                
                imageViews[safe:idx]?.pin_setImage(from: user.content.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())

            }
            
            
            pec_image_view.pin_setImage(from: model.feature_image.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            titleLabel.text = model.feature_name
            
            content_label.text = model.supplementStr
            
            
        }
    }
    
    
}

extension ST_AttractedSupplementCell{
    
    @objc fileprivate func add_pec_handle(){
        
        
        push_to_push_peculiarity_controller(navigationController: self.parentViewController?.navigationController)
        
    }
     
    
    @objc fileprivate func preview_handle(gez:UITapGestureRecognizer){
        
        
        guard let v = gez.view as? UIImageView, let idx = imageViews.firstIndex(of: v),let user = model.feature_user[safe:idx] else { return  }
        
        
        let vc = ST_PairsCenterViewController()
        
        vc.accid = user.accid

        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
        
        /*
        var array = [ST_MWPhoto]()
        
        for item_model in model.feature_user{
            
            guard let url = item_model.content.url , let media = ST_MWPhoto.init(url: url) else{continue}
            
            array.append(media)
            
        }
        
        guard let browser = ST_MWPhotoBrowser.init(photos: array) else{
            return
        }
        
        browser.displayActionButton = false
        
        browser.setCurrentPhotoIndex(idx.uInt)
        
        let nav = UINavigationController.init(rootViewController: browser)
        
        GlobalPresent(toController: nav)
        
        */
        
    }
}
